package com.sea.wechat.controller;

import com.alibaba.fastjson.JSONObject;
import com.sea.wechat.util.ConfigUtil;
import com.sea.wechat.util.HttpUtils;
import com.sea.wechat.util.JedisUtil;
import com.sea.wechat.util.RedisKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**   
 * @ClassName:     ServerIpController   
 * @Description:   获取服务器ip地址
 * @author:        wl
 * @date:          2017年7月31日 下午5:52:12     
 */  
@RestController
public class ServerIpController {

	private static final Logger logger = LoggerFactory.getLogger(AccessTokenController.class);
	
	@Autowired
	private ConfigUtil configUtil;
	
	@RequestMapping(value="getServerIP")
	public String getServerIP(){
		String url = configUtil.getGetServerIp();
		if(JedisUtil.exists(RedisKey.ACCESS_TOKEN)){
			url = url+JedisUtil.get(RedisKey.ACCESS_TOKEN);
		}else {
			String access_token = HttpUtils.HttpsClientGet(configUtil.getGetAccessTokenUrl(), null, null);
			url = url+access_token;
		}
		String result = HttpUtils.HttpsClientGet(url,null,null);
		JSONObject obj = JSONObject.parseObject(result);
		logger.info("getServerIP:请求地址:"+url+"===返回信息:"+result);
		if(obj.containsKey(RedisKey.IP_LIST)){
			return obj.getString(RedisKey.IP_LIST);
		}else {
			return result;
		}
	}
}
